<?php
 
include_once 'controller/JO_topicController.php';
 //include_once( "DataProvider/DataProvider.php" );
?>

<?php
	function JO_topic_multi_level($sParent, $sprefix = "",$temp) {

		if($sParent == NULL)
			$rs_result = topicController::JO_topic_getList("parent",0);
		else
			$rs_result = topicController::JO_topic_getList("parent",$sParent);
		
		while ($row = $rs_result->fetch_array()){
				
				if($temp !=null && $row["key"]==$temp)
					echo "<option value='".$row["key"]."' selected='selected'>".$sprefix.$row["subject"]."</option>";
				else
					echo "<option value='".$row["key"]."'>".$sprefix.$row["subject"]."</option>";
				
				JO_topic_multi_level($row["key"],$sprefix."....",$temp);
		}
		
	}
?>

<script type="text/javascript">
	/*
	 * Select item in below list topic to modify it
	 */
	function item_selected(n_id){
		window.location.assign("?SO_unit=06_topic&SO_chude=" + n_id);
	}

	/*
	 * method to excute click event of delete button in below list topic
	 */
	function delete_click(chk){

		var sel = 1;
		if(chk.value == null){
			var sel = 0;
			//alert(chk.value);
			for(var i=0;i<chk.length;i++){
				if(chk[i].checked){
					sel++;
				}
			}
		}

		if (sel ==0) { alert("Bạn hãy chọn đối tượng muốn xóa!");return false; }
		if(confirm("Bạn muốn xóa các sản phẩm đã chọn?")){
			document.forms["frm_topic_list"].submit();
		}
	}

	function check_validation(input_name,lbl_validation) {
		if( input_name.value == "" || input_name.value.length < 3 ){
			$('#'+lbl_validation).fadeIn("slow", "linear");
			$(input_name).focus;
		}else {
			$('#'+lbl_validation).fadeOut();
		}
	} 

	/*
	 * Kiem tra du lieu nhap vao cua form
	 */
	function checkInput(param)
	{
		if (param.txt_subject.value=="")
		{
			$('#lbl_subject_validation').fadeIn("slow", "linear");
			param.txt_subject.focus();
			return false;
		}
		return true;
	}
</script>

<?php
	if(isset($_GET["SO_chude"])){
		$rs_result = topicController::JO_topic_get($_GET["SO_chude"]);
		$row = $rs_result->fetch_array();
	} 
?>

<div style="margin-left: 0px;" class="bar_720i" >
	
	<div style="" class="title_720i">
		<h1 
			style="width:696px;"
			class="h1i"
		>K-PopZone
		</h1>
	</div>
	
	<div class="content720i">
		<div class="mar8i"></div>
		
		<div style="width: 704px;float: left;margin-bottom: 8px;">
			<div class="JO_Kpop_itemi JO_no_background">
				<div class="JO_kpop_item_titlei">
					Topic
				</div>
				<div class="mar8i"></div>
				<form 
					action	= "controller/JO_navigation.php"
					target	= "JO_integration_asynchronous"
					method	= "post"
					onsubmit= "return checkInput(this);"
				>
					<input 
						type	= "hidden" 
						id		= "SO_query"
						name	= "SO_query"
						<?php
							if(!isset($_GET["SO_chude"]))
								echo "value	= \"topic_insert\"";
							else
								echo "value	= \"topic_update\"";
						?>
					/>
					
					<input 
						type	= "hidden" 
						id		= "JO_controller"
						name	= "JO_controller"
						value	= "topic_controller"
					/>
					
					<input 
						type	= "hidden" 
						id		= "SO_id"
						name	= "SO_id"
						<?php
							if(isset($_GET["SO_chude"]))
								echo "value	= \"".$_GET["SO_chude"]."\"";
							else
								echo "value	= \"0\"";
						?>
					/>
					
					<table class="JO_tablei_input" style="margin-top:0px;">
						<tr>
							<th width="100px">Subject</th>
							<td>
								<input 
									id		= "txt_subject"
									name	= "txt_subject"
									type	= "text"
									class	= "JO_txt_input"
									onblur	= "check_validation(this,'lbl_subject_validation');"
									<?php
										if( isset($_REQUEST[ "SO_chude" ] )) {
											echo "value=\"".$row["subject"]."\"";
										} 
									?>
								/>
								<label
									id		= "lbl_subject_validation"
									class	= "JO_lbl_validation"
								> Bạn phải nhập vào tên chủ đề và nhiều hơn 2 ký tự 
								</label>
							</td>
						</tr>
						
						<tr>
							<th class="align_top">Description</th>
							<td>
								<textarea 
									id		= "ta_description"
									name	= "ta_description"
									cols	= "false"
									class	= "JO_ta_input"
								><?php
									if( isset($_REQUEST[ "SO_chude" ] )) {
										echo html_entity_decode($row["description"],ENT_QUOTES,"UTF-8");
									} 
								?></textarea>
								<script type="text/javascript">
									$("#ta_description").autoResize({
										// On resize:
										onResize : function() {
								  			$(this).css({opacity:1});
								  		},
										// After resize:
										animateCallback : function() {
											$(this).css({opacity:1});
										},
										// Quite slow animation:
										animateDuration : 300,
										// More extra space:
										extraSpace : 16
									});
								</script>
							</td>
						</tr>
						
						<tr>
							<th>Parent</th>
							<td>
								<select
									id		= "cb_parent"
									name	= "cb_parent"
									class	= "JO_cb_input"
								>
									<option
										value="0"
										<?php
										if(!isset($_GET["SO_chude"]) || (isset($_GET["SO_Chu_De"]) && $row["parent"]==0))
											echo "selected=\"selected\"";
										?>
									></option>
									
									<?php
									$temp = null;
									if(isset($row[ "key" ]))
										$temp = $row[ "parent" ];
									JO_topic_multi_level($sParent=null, $sprefix = "",$temp);
									?>
									
								</select>
							</td>
						</tr>
						
						<tr class= "last_row">
							<th class= "last_row"></th>
							<td class= "last_row">
								<input 
									id		= "butt_subject"
									name	= "butt_subject"
									type	= "submit"
									class	= "JO_butt_input"
									<?php
									if(!isset($_GET["SO_chude"]))
										echo "value	= \"Insert Topic\"";
									else
										echo "value	= \"Update Topic\"";
									?>
								/>
							</td>
						</tr>
						
					</table>
				</form>
				
				<div class="JO_kpop_item_titlei">
					List of Topics
				</div>
				<div class="mar8i"></div>
				
				<form
					name="frm_topic_list"
					action	= "controller/JO_navigation.php"
					target	= "JO_integration_asynchronous"
					method	= "post"
				>
					<input 
						type	= "hidden" 
						id		= "SO_query"
						name	= "SO_query"
						value	= "topic_delete"
					>
					<input 
						type	= "hidden" 
						id		= "JO_controller"
						name	= "JO_controller"
						value	= "topic_controller"
					/>
					
					<table class="JO_tablei_input">
						<tr>
							<th width= "28px"	>No.</th>
							<th width= "180px"	>Subject</th>
							<th width= "180px"	>Description</th>
							<th width= "180px"	>Parent</th>
							<th width= "68px"	>Insert Date</th>
							
							<th class="aligh_center">
								<a 
									href	= "javascript: delete_click(document.frm_topic_list.chk_delete);"
									title	= "Xóa"
								>
									<img 
										alt="Edit"
										src="JO_images/JO_23_icon_delete_on.png"
										class="butt_image"
									/>
								</a>
							</th>
							
						</tr>
						
						<?php
							$rs_result = topicController::JO_topic_getList("all",-1);
							$i_stt = 1;
							while ($row = $rs_result->fetch_array()){
								$datetime=new DateTime($row["insert"]);
						?>
						<tr>
							<td onclick="item_selected(<?=$row["key"];?>);"><?=$i_stt;									?></td>
							<td onclick="item_selected(<?=$row["key"];?>);"><?=$row["subject"] 							?></td>
							<td onclick="item_selected(<?=$row["key"];?>);"><?=limit_text($row["description"],34)."...";?></td>
							<td onclick="item_selected(<?=$row["key"];?>);"><?=$row["parent_name"]						?></td>
							<td onclick="item_selected(<?=$row["key"];?>);"><?=$datetime->format("d/m/Y");				?></td>
							<td>
								<input 
									type	= "checkbox"
									id		= "chk_delete"
									name	= "chk_delete[]"
									value	= "<?=$row["key"];?>"
								/>
							</td>
						</tr>
						<?php
								$i_stt++; 
							}
						?>
						<tr class= "last_row">
							<td colspan="5" class="paging_row">
								<a href="#" class="selected">1</a> 
								<a href="#">2</a>
								<a href="#">3</a>
								<a href="#">4</a>
								<a href="#">5</a>
								<a href="#">6</a>
							</td>
						</tr>
						
					</table>
				</form>
			</div>
		</div>
		
	</div>
	
</div>